// JavaScript Document
$(function () {
	initModify();
	//提交
	$(".submit").on("click",function(){
		checkModify();
	});
	//图片文件内容改变
	$(".uploadFile").on("change",function(){
		$(".imgurl").val($(this).val());
        var reader = new FileReader();
        reader.readAsDataURL(this.files[0]);
        reader.onload = function(e){
            var strbase64 = e.target.result;
            var a = strbase64.split("base64,");
            $('.file_base64').val(a[1]);
        };
        preImg();//显示图片
	});
	//点击浏览图片
	$(".scan").on("click",function(){
		$(".uploadFile").click();	
	});
});
//初始化
function initModify(){
	CKEDITOR.replace( 'crraeContent' , { height: '240px', 
		width: '480px',
		toolbar: [['Source','-','Preview'],
		          ['Cut','Copy','Paste','PasteText','PasteFromWord'],
		          ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
		          ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
		          ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
		           ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
		           ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
		           ['Link','Unlink'],
		          ['Image','Table','HorizontalRule','Smiley'],
		           ['Styles','Format','Font','FontSize'],
		           ['TextColor','BGColor']],
		extraPlugins: 'codesnippet',
		uiColor: '#f3f3f3'
	} );
	getInfo(getQueryString("id"));	
}
//获取详情
function getInfo(id){
	var str = 'crraeUuid='+encodeURIComponent(id);
	getOData(str,"coreRideActive/views",{fn:function(oData){
		if(oData.code == 1) {
			$(".crraeUuid").val(oData.data.crraeUuid || "");
			$(".crraeTitle").val(oData.data.crraeTitle || "");
			CKEDITOR.instances.crraeContent.setData(oData.data.crraeContent);
			$(".crraeValue").val(oData.data.crraeValue || "");
			var imgUrl="/"+oData.data.list[0].cratmDir+"/"+oData.data.list[0].cratmFileName;
			$(".imgurl").val(imgUrl || "");
			getImageWidthHeight(urlfile+"/coreAttachment/image/get/thumb/"+oData.data.list[0].cratmUuid,function(realWidth,realHeight){
				var width = 0;
				var height = 200;
				//如果真实的宽度大于浏览器的宽度就按照200显示
				if(realHeight>=height){
					width = realWidth/realHeight*height;
					$(".preimg").css("width",width).css("height",height);
				}
				else{//如果小于浏览器的宽度按照原尺寸显示
					$(".preimg").css("width",realWidth+'px').css("height",realHeight+'px');
				}
				$(".preimg").attr("src",urlfile+"/coreAttachment/image/get/thumb/"+oData.data.list[0].cratmUuid);
				$(".preimg").attr("data-filename",imgUrl);
			});
		} else {
			alert(data.errMsg);
		}
	}});
}
//检查提交
function checkModify(){
	if($.trim($(".crraeTitle").val()) == ""){
		alert("标题不能为空，请填写完再提交！");
		$(".crraeTitle").focus();
		return false;
	}
	if($.trim(CKEDITOR.instances.crraeContent.getData()) == ""){
		alert("内容不能为空，请填写完再提交！");
		return false;
	}
	if($.trim($(".crraeValue").val()) == ""){
		alert("价格不能为空，请填写完再提交！");
		$(".crraeValue").focus();
		return false;
	}
	if($.trim($(".imgurl").val()) == ""){
		alert("附件不能为空，请上传附件再提交！");
		return false;
	}
	
	var r=confirm("是否确认修改？");
	if (r==true){
		var msgObject = parent.layer.msg('处理中，请等待……', {
			icon: 16,
			shade: 0.4,
			time: waitImgTime //（如果不配置，默认是3秒）
		}, function(index){
			//do something
			parent.layer.close(index);
		});
		if($.trim($(".imgurl").val()) == $(".preimg").attr("data-filename")){
			Modify(msgObject);
		}else{
			uploadPic(getQueryString("id"),msgObject);
		}
	}
}

//上传图片
function uploadPic(cratmBusUuid,msgObject){
	$(".cratmBusUuid").val(cratmBusUuid);
	$("#userForm").ajaxSubmit({
        //定义返回JSON数据，还包括xml和script格式
        type: "POST",
        dataType:'json',
        url: urlfile + "coreAttachment/upload/update",
        success: function(strdata) {
        	if(strdata.success==true){
        		Modify(msgObject);
        	}else {
            	alert(strdata.errMsg);
            }
        }
    });
}

//提交
function Modify(msgObject){
	var crraeUuid = getQueryString("id");
	var crraeTitle = $(".crraeTitle").val();
	var crraeContent = CKEDITOR.instances.crraeContent.getData();
	var crraeValue = $(".crraeValue").val();
	var str = 'crraeUuid='+encodeURIComponent(crraeUuid)+'&crraeTitle='+encodeURIComponent(crraeTitle)+'&crraeContent='+encodeURIComponent(crraeContent)+'&crraeValue='+encodeURIComponent(crraeValue);
	getOData(str,"coreRideActive/update/coreRideActive",{
		fn:function(oData){
			window.parent.refreshList();
			alert("修改成功！");		
		},
		fnerr:function(oData){
			parent.layer.close(msgObject);
		}
	});
}